package com.wlyuan.open.remote.consumer;

import com.wlyuan.core.domain.event.DomainEventPublisher;
import com.wlyuan.open.infrastructure.event.OrderChanged;
import com.wlyuan.open.remote.converter.WaybillMessageConverter;
import com.wlyuan.open.remote.message.OrderMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
@RequiredArgsConstructor
public class WaybillPriceConfirmedConsumer {

    private final WaybillMessageConverter messageConverter = new WaybillMessageConverter();

    @RabbitListener(bindings = {
            @QueueBinding(value = @Queue(value = "queue.open.pusher.waybill.price.confirmed"),
                    exchange = @Exchange(value = "exchange.topic.waybill"), key = "WaybillPriceConfirmed")
    })
    public void receiveWaybillPriceConfirmed(String message) {
        logger.info("收到运单费用确认信息接收：{}", message);
        OrderMessage orderSignedMessage = messageConverter.convert(message);
        if (orderSignedMessage != null) {
            orderSignedMessage.getOrder().setAcceptTime(null);
            //转发
            DomainEventPublisher.publish(new OrderChanged(orderSignedMessage.getOrder(), "WaybillPriceConfirmed"));
        }
    }
}
